namespace Xmef.Engine
{
    using System;
    using Xmef.Interfaces;
    using Xmef.Library;
    using Xmef.Serializer;

    [Serializable]
    public class PluginConfigurationData : PluginIdentity, IXmlPluginConfiguration, IEquatable<IXmlPluginConfiguration>, IPluginIdentity
    {
        private string _name;
        private string _publicKeyToken;
        internal static readonly IXmlRuntimeFormatter Formatter = new XmlFormatter();
        private static readonly IXmlStringSerializer<IXmlPluginConfiguration> XmlStringSerializer = new XmlUnicodeStringSerializer<IXmlPluginConfiguration>();

        private PluginConfigurationData()
        {
        }

        internal PluginConfigurationData(IXmlPluginConfiguration plugin) : base(plugin)
        {
            this._name = plugin.Name;
            this._publicKeyToken = plugin.PublicKeyToken;
        }

        bool IEquatable<IXmlPluginConfiguration>.Equals(IXmlPluginConfiguration plugin)
        {
            return (Id == plugin.Id);
        }

        public override string ToString()
        {
            return XmlStringSerializer.Serialize(this, Formatter);
        }

        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._name = value;
            }
        }

        public string PublicKeyToken 
        {
            get 
            {
                return this._publicKeyToken;
            }
            set
            {
                this._publicKeyToken = value;
            }
        }

        private class XmlFormatter : XmlTypeSerializer<PluginConfigurationData>
        {
            public override void AddOverrides(IXmlTypeFormatter formatter)
            {
                AddType(formatter, "plugin");
                AddAttribute(formatter, "Name", "assemblyName");
                AddAttribute(formatter, "PublicKeyToken", "publicKeyToken");
            }
        }
    }
}

